package com.lbd.smsplatform.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.smsplatform.bean.SmsLog;
import com.lbd.smsplatform.dao.SmsLogRepository;
import com.lbd.smsplatform.service.SmsLogService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by wang on 2016/9/23.
 */
@Service
@Transactional
public class SmsLogServiceImpl extends BaseServiceImpl<SmsLog, SmsLogRepository> implements SmsLogService {

    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    SmsLogRepository smsLogRepository;


    @Override
    public String buildJpql(SmsLog smsLog, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  SmsLog t   ");
        if (smsLog != null) {
            if (StringUtils.isNotBlank(smsLog.getSender())) {
                jpqlBuilder.append(" and  sender like :sender");
                parameter.put("sender", "%" + smsLog.getSender() + "%");
            }
            if (StringUtils.isNotBlank(smsLog.getSenderIp())) {
                jpqlBuilder.append(" and  senderIp like :senderIp");
                parameter.put("senderIp", "%" + smsLog.getSenderIp() + "%");
            }
            if (StringUtils.isNotBlank(smsLog.getRecipient())) {
                jpqlBuilder.append(" and  recipient like :recipient");
                parameter.put("recipient", "%" + smsLog.getRecipient() + "%");
            }
            if (null != smsLog.getMsgType()) {
                jpqlBuilder.append(" and  msgType = :msgType");
                parameter.put("msgType", smsLog.getMsgType());
            }
        }
        return jpqlBuilder.toString();
    }


}
